package com.zzyl.serve.service.impl.admin;

import java.util.HashMap;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.serve.dto.UserLoginRequestDto;
import com.zzyl.serve.service.admin.WechatService;
import com.zzyl.serve.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.admin.FamilyMemberMapper;
import com.zzyl.serve.domain.admin.FamilyMember;
import com.zzyl.serve.service.admin.IFamilyMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.Map;

/**
 * 老人家属Service业务层处理
 * 
 * @author 王崇琪
 * @date 2025-03-12
 */
@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService
{
    @Autowired
    private FamilyMemberMapper familyMemberMapper;
    @Autowired
    private WechatService wechatService;
    @Autowired

    private TokenService tokenService;

    /**
     * 查询老人家属
     * 
     * @param id 老人家属主键
     * @return 老人家属
     */
    @Override
    public FamilyMember selectFamilyMemberById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询老人家属列表
     * 
     * @param familyMember 老人家属
     * @return 老人家属
     */
    @Override
    public List<FamilyMember> selectFamilyMemberList(FamilyMember familyMember)
    {
        return familyMemberMapper.selectFamilyMemberList(familyMember);
    }

    /**
     * 新增老人家属
     * 
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int insertFamilyMember(FamilyMember familyMember)
    {
        return save(familyMember) ? 1 : 0;
    }

    /**
     * 修改老人家属
     * 
     * @param familyMember 老人家属
     * @return 结果
     */
    @Override
    public int updateFamilyMember(FamilyMember familyMember)
    {
        return updateById(familyMember) ? 1 : 0;
    }

    /**
     * 批量删除老人家属
     * 
     * @param ids 需要删除的老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除老人家属信息
     * 
     * @param id 老人家属主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public LoginVo login(UserLoginRequestDto userLoginRequestDto) {
        // TODO 1.拿到微信小程序传来的参数
        String code = userLoginRequestDto.getCode();
        //调用第三方接口得到openid
        String openid = wechatService.getOpenid(code);
        //调用第三方接口得到手机号
        String phoneCode = userLoginRequestDto.getPhoneCode();
        String phone = wechatService.getPhone(phoneCode);
        //根据openid判断是否是添加还是修改
        FamilyMember familyMember = getOne(new LambdaQueryWrapper<FamilyMember>().eq(FamilyMember::getOpenId, openid));
        if(ObjectUtils.isNull(familyMember)){
            //说明是新用户,之前的familyMember就是null,我们重新赋值
            familyMember=new FamilyMember();
            //设置手机号
             familyMember.setPhone(phone);
            //设置初始昵称
            familyMember.setName("花开富贵"+DateUtils.getTime());
            //设置openid
            familyMember.setOpenId(openid);
            save(familyMember);

        }
        else{
            //老用户更新手机号
            familyMember.setPhone(phone);
            updateById(familyMember);
        }
        //将用户id存到token中,并且将数据返回给前端
        // 6.将用户id存入token,返回
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", familyMember.getId());
        claims.put("nickName", familyMember.getName());
        //注意有2个同名的方法不要搞错
        //TODO 根据id和昵称来设置token
        String token = tokenService.createToken(claims);
        // TODO 拼接返回值一共有2个属性
        LoginVo loginVo = new LoginVo();
        loginVo.setToken(token);
        loginVo.setNickName(familyMember.getName());
        return loginVo;

    }
}
